// npm i vue-router
import Vue from 'vue'
import VueRouter from 'vue-router'
// 注册路由
Vue.use(VueRouter)
// 配置路由
const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login',// 重定向:重新指向其它path,会改变网址
        },
        {
            path: '/login',
            name:'login',
            component:()=> import('@/views/login')
        },
        {
            path:'/register',
            name:'register',
            component:()=> import('@/views/register')
        },
        {
            path:'/articleDetail/:id',
            name:'articleDetail',
            component:()=> import('@/views/articleDetail')
        },
        {
            path:'/footer',
            name:'footer',
            redirect: "/article",
            component:()=> import('@/components/footer'),
            children:[
                {
                    path:'/article',
                    name:'article',
                    component:()=> import('@/views/article'),
                },
                {
                    path:'/collection',
                    name:'collection',
                    component:()=> import('@/views/collection'),
                },
                {
                    path:'/favorite',
                    name:'favorite',
                    component:()=> import('@/views/favorite'),
                },
                {
                    path:'/myself',
                    name:'myself',
                    component:()=> import('@/views/myself'),
                },
            ]
        },
        {
            path: "*",
            component:()=>import('@/views/NotFound') ,
          },

    ],
})
import { getToken } from "@/utils/token";

router.beforeEach((to, from, next) => {
    let token = getToken()
    if (token) {
        if (to.path=='/login'||to.path=='/register') {
            next(
                {
                    name:'footer'
                }
            )
        }
    }else{
        if (to.path=='/login'||to.path=='/register') {
            next()
        }
    }
    next()
  })



export default router
